package com.ds.infrastructure.audit.collector.service;

import com.ds.infrastructure.audit.collector.entity.RateAccess;

/**
 * 作用：
 *
 * @author WeiShaoying
 * @date 2020/8/5
 */
public interface IRateService {

    /**
     * 获得Luau脚本
     *
     * @return
     */
    default String getLuaScript() {
        return "local c" +
                "\nc = redis.call('get', KEYS[1])" +
                "\nif c and tonumber(c) > tonumber(ARGV[1]) then" + //
                "\nreturn c;" +
                "\nend" +
                "\nc = redis.call('incr', KEYS[1])" +
                "\nif tonumber(c) == 1 then" +
                // 从第一次开始设置过期
                "\nredis.call('expire', KEYS[1],ARGV[2])" +
                "\nend" +
                "\nreturn c;";
    }


    /**
     * 判断是否可以访问
     *
     * @param rateAccess
     * @return
     */
    boolean canAccess(RateAccess rateAccess);

}
